package com.prism.e4.rcp.ui.parts;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.ChangeEvent;
import org.eclipse.core.databinding.observable.IChangeListener;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.e4.ui.di.Focus;
import org.eclipse.e4.ui.di.Persist;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

import com.prism.e4.rcp.listeners.PrismLineStyleListener;
import com.prism.e4.rcp.ui.EditorService;

public class E4PrismEditor {

	@Inject
	public MDirtyable dirty;
	
	//@Inject
	//public IEclipseContext context;

	@Inject
	public E4PrismEditor() {
	}

	public StyledText styledText = null;
	
	public EditorService editorService;

	private DataBindingContext ctx = new DataBindingContext();

	IChangeListener listener = new IChangeListener() {
		@Override
		public void handleChange(ChangeEvent event) {

		}
	};
	//private SupportedFile file;

	// IObservableList providers

	@PostConstruct
	public void postConstruct(Composite parent, MPart part, MApplication application) {
		// String string = part.getPersistedState().get(PrismFile.FIELD_ID);
		// System.out.println("String got by persistedState." + string);
		// Long idofPrismFile = Long.valueOf(string);

		// PrismFileService prismFileService = new PrismFileService();
		// file = FileServiceFactory.getInstance().getSupportedFile(
		// Long.parseLong(string));

		//SourceViewer viewer = new SourceViewer(parent, null, 0);
		
		//IUndoManager undoManager = new TextViewerUndoManager(25);
		//viewer.setUndoManager(undoManager);
		//undoManager.connect(viewer);
		
		//System.out.println("CreateControls called");

		parent.setLayout(new GridLayout(1, false));
		styledText = new StyledText(parent, SWT.BORDER | SWT.V_SCROLL
				| SWT.H_SCROLL | SWT.MULTI);
		styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true,
				1, 1));
		styledText.setAlwaysShowScrollBars(false);

		styledText.addLineStyleListener(new PrismLineStyleListener(styledText));
		try {
			// styledText.setText(" ");
		} catch (Exception e) {
			e.printStackTrace();
		}
		styledText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {

				dirty.setDirty(true);
			}
		});
		editorService = new EditorService(styledText);
	}

	@Persist
	public void save(MDirtyable dirty) {
		// PrismFileService.savePrismFile(file);
		dirty.setDirty(false);
	}

	private void updateUserInterface() {
		if (styledText != null && !styledText.isDisposed()) {
			IObservableList providers = ctx.getValidationStatusProviders();
			for (Object o : providers) {
				Binding b = (Binding) o;
				b.getTarget()
						.removeChangeListener(
								(org.eclipse.core.databinding.observable.IChangeListener) listener);
			}
			ctx.dispose();

			// IObservableValue target =
			// WidgetProperties.text(SWT.Modify).observe(styledText);
			// IObservableValue model = BeanProperties.value
		}
	}

	@Focus
	public void onFocus() {
		styledText.setFocus();
	}

	public void newModel() {
		try {
			read(new StringReader(""), "");
		} catch (IOException ex) {

		}
	}

	public void read(Reader stringReader, Object object) throws IOException {
		String text = "";
		int intValueOfChar;
		while ((intValueOfChar = stringReader.read()) != -1) {
			text += (char) intValueOfChar;
		}
		stringReader.close();
		styledText.setText(text);

	}
}
